/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Author: Kyle Scholz      http://kylescholz.com/
 * Copyright: 2006-2007
 */

/**
 * Timer
 * 
 * @author Kyle Scholz
 * 
 * @version 0.3
 * 
 * Timer with very rough throttle.
 * 
 * @author Kyle Scholz
 */
var Timer = function( timeout ) {
	this.init( timeout , 10000);
};
Timer.prototype = {

	/*
	 * Initialize the Timer with the indicated timeout.
	 * 
	 * @param {Object} timeout
	 */
	init: function( timeout , stopTimer) {
		this['timer'];
		this['TIMEOUT'] = timeout;
		this['BASE_TIMEOUT'] = timeout;
		this['interupt'] = true;
		this['subscribers'] = new Array();
		this['ontimeout'] = new EventHandler( this,
			// notify subscribers and restart timer
			function() {
				this.notify();
				if ( !this.interupt ) { this.start(); }
			}
		);
		this['stopTimer'] = stop();
		this.counter = 0;
		this.max = 200;
	},

	/*
	 * Start the Timer.
	 */
	start: function() {
		this['interupt']=false;
		this['timer'] = window.setTimeout(this.ontimeout,this['TIMEOUT']);
	},

	/*
	 * Stop the Timer.
	 */
	stop: function() {
		this['interupt']=true;
	},

	/*
	 * Subscribe an observer.
	 */
	subscribe: function( observer ) {
		this.subscribers.push( observer );
	},

	/*
	 * Notify observers when a tick event has occured.
	 */
	notify: function() {
		if(this.counter < this.max){
			this.counter++;
			for( var i=0; i<this.subscribers.length; i++ ) {
				var nextTimeout = this.subscribers[i].update();
				if ( nextTimeout == false ) {
					this.stop();
				} else if ( nextTimeout != null ) {
					this['TIMEOUT']=nextTimeout;
				}
			}	
			
			if(this.counter == this.max/20){
				document.getElementById("percent").src="5.png";
			}
			else if(this.counter == this.max/10){
				document.getElementById("percent").src="10.png";
			}
			else if(this.counter == ((this.max * 15) /100)){
				document.getElementById("percent").src="15.png";
			}
			else if(this.counter == ((this.max * 20) /100)){
				document.getElementById("percent").src="20.png";
			}
			else if(this.counter == ((this.max * 25) /100)){
				document.getElementById("percent").src="25.png";
				document.getElementById("infoImg").src="2.png";
			}
			else if(this.counter == ((this.max * 30) /100)){
				document.getElementById("percent").src="30.png";
			}
			else if(this.counter == ((this.max * 35) /100)){
				document.getElementById("percent").src="35.png";
			}
			else if(this.counter == ((this.max * 40) /100)){
				document.getElementById("percent").src="40.png";
			}
			else if(this.counter == ((this.max * 45) /100)){
				document.getElementById("percent").src="45.png";
			}
			else if(this.counter == ((this.max * 50) /100)){
				document.getElementById("percent").src="50.png";
				document.getElementById("infoImg").src="3.png";
			}
			else if(this.counter == ((this.max * 55) /100)){
				document.getElementById("percent").src="55.png";
			}
			else if(this.counter == ((this.max * 60) /100)){
				document.getElementById("percent").src="60.png";
			}
			else if(this.counter == ((this.max * 65) /100)){
				document.getElementById("percent").src="65.png";
			}
			else if(this.counter == ((this.max * 70) /100)){
				document.getElementById("percent").src="70.png";
			}
			else if(this.counter == ((this.max * 75) /100)){
				document.getElementById("percent").src="75.png";
				document.getElementById("infoImg").src="4.png";
			}
			else if(this.counter == ((this.max * 80) /100)){
				document.getElementById("percent").src="80.png";
			}
			else if(this.counter == ((this.max * 85) /100)){
				document.getElementById("percent").src="85.png";
			}
			else if(this.counter == ((this.max * 90) /100)){
				document.getElementById("percent").src="90.png";
			}
			else if(this.counter == ((this.max * 95) /100)){
				document.getElementById("percent").src="95.png";
			}
			else if(this.counter == this.max - 2){
				document.getElementById("percent").src="100.png";
			}
			
		}
		else{
			this.stop();
			document.getElementById("pleaseWait").style.display='none';
		}
	}
}